package com.fingard.dsp.bank.directbank.ccblifeicbc02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 余额查询
 * 
 * 需求描述：
 * 		1、指令代码：QACCBAL -- 支持单个账户或多账户余额查询
 * @author Cruise
 * @date 2018/10/23
 */
public class CCBLifeICBC02Balance extends CCBLifeICBC02Base implements IBalance {
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        retAts.writeStart();
        
        try {
        	CCBLifeICBC02Header tmpBankHeader = new CCBLifeICBC02Header();
        	tmpBankHeader.transCode = "QACCBAL";
        	tmpBankHeader.fSeqNo = p_req7506.reqHeader.transSeqID;
            
            int rdCount = 0;
            while (p_req7506.readRecord()) {
                rdCount++;
                
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, tmpBankHeader);
                xmlWriter.writeElementString("TotalNum", String.valueOf(p_req7506.detailCount));
                xmlWriter.writeElementString("BLFlag", "0");
                xmlWriter.writeElementString("SynFlag", "1");
                boolean isSubAct = p_req7506.readedDetail.higherLevelAct.length() > 0;
                xmlWriter.writeStartElement("rd");
                xmlWriter.writeElementString("iSeqno", String.valueOf(rdCount));
                xmlWriter.writeElementString("AccNo", isSubAct ? p_req7506.readedDetail.higherLevelAct : p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("CurrType", convertCurToBank(p_req7506.readedDetail.actCur));
                xmlWriter.writeElementClosed("ReqReserved3");
                xmlWriter.writeElementString("AcctSeq", isSubAct ? p_req7506.readedDetail.actNum : "");
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        if (isActiveRetCode(tmpRetCode)) {
                            while (tmpBkReader.read("/CMS/eb/out/rd")) {
                                if (tmpBkReader.readedXElement.getSingleText("iRetCode").equals("0")) {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                    tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrType"));
                                    tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AccNo");

                                    String tmpUsableBalance = "";
                                    if (tmpBkReader.readedXElement.getSingleText("UsableBalance") != null) {
                                        tmpUsableBalance = tmpBkReader.readedXElement.getSingleText("UsableBalance").trim();
                                    }
                                    if (tmpUsableBalance.length() > 0) {
                                        tmpRetDetail.availBal = StringHelper.amountDivide100(tmpUsableBalance);
                                    } else {
                                        tmpRetDetail.availBal = "0.00";
                                    }

                                    String tmpBalance = "";
                                    if (tmpBkReader.readedXElement.getSingleText("Balance") != null) {
                                        tmpBalance = tmpBkReader.readedXElement.getSingleText("Balance").trim();
                                    }
                                    if (tmpBalance.length() > 0) {
                                        tmpRetDetail.currBal = StringHelper.amountDivide100(tmpBalance);
                                    } else {
                                        tmpRetDetail.currBal = tmpRetDetail.availBal;
                                    }
                                    String frozenBal = tmpBkReader.readedXElement.getSingleText("FrzAmt").trim();
                                    if (StringHelper.hasAnyChar(frozenBal)) {
                                    	tmpRetDetail.frozenBal = StringHelper.amountDivide100(frozenBal);
                                    }
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                                }
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            if (tmpBkReader.getSimpleText("iRetMsg") != null && tmpBkReader.getSimpleText("iRetMsg").length() > 0) {
                                retAts.writeRespInfo(tmpBkReader.getSimpleText("iRetMsg"));
                            } else {
                                retAts.writeRespInfo(tmpBkReader.getSimpleText("RetMsg"));
                            }
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            }
            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        } catch (Exception ex) {
        	ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }
        
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
